package net.borderwars.util;

import java.text.DateFormat;
import java.util.Date;
import java.util.logging.*;

/**
 * @author Eric
 *         Date: Jul 28, 2005
 *         Time: 2:50:57 PM
 */
public class LogFormat extends Formatter {
    private Date tmpDate;
    private DateFormat df;
    private StringBuffer rtn = new StringBuffer ();

    public String format (LogRecord record) {
        rtn = new StringBuffer (rtn.capacity ());
        long time = record.getMillis ();
        tmpDate = new Date (time);

        //df = DateFormat.getDateInstance(DateFormat.SHORT);
        df = DateFormat.getTimeInstance (DateFormat.SHORT);
        rtn.append ("[");
        String sdf = df.format (tmpDate);
        rtn.append (sdf);

        rtn.append ("][");
        rtn.append (record.getThreadID ());
        rtn.append ("]");
        String totalClassName = record.getSourceClassName ();
        totalClassName = totalClassName.substring (15);
        rtn.append (totalClassName);
        rtn.append (":");
        rtn.append (record.getSourceMethodName ());
        rtn.append (" ");
        rtn.append (record.getMessage ());
        rtn.append ("\n");
        return rtn.toString ();
    }

    public static void setupLogging () {
        ConsoleHandler c1 = new ConsoleHandler ();
        c1.setLevel (Level.FINEST);
        c1.setFormatter (new LogFormat ());
        Logger l = Logger.getLogger ("");

        Handler handlers[] = l.getHandlers ();
        for (Handler h : handlers) {
            l.removeHandler (h);
        }
        l.addHandler (c1);
    }
}
